package com.zhongmeng.rxjava2test.presenter


import com.zhongmeng.rxjava2test.base.BaseModle
import com.zhongmeng.rxjava2test.base.BasePresenter
import com.zhongmeng.rxjava2test.bean.Banner
import com.zhongmeng.rxjava2test.bean.BaseBean
import com.zhongmeng.rxjava2test.bean.Notice
import com.zhongmeng.rxjava2test.bean.Work
import com.zhongmeng.rxjava2test.modle.HomeModleImpl
import com.zhongmeng.rxjava2test.retrofit.ApiServiceManager
import com.zhongmeng.rxjava2test.utils.SaveUserInfo
import com.zhongmeng.rxjava2test.view.HomeView

/**
 * author :rcw.
 * date :2017/10/12 11:21.
 */

class HomePresenterImpl<T>(private var homeView: HomeView<T>?) : BasePresenter(), HomePresenter, BaseModle.OnRequestListener<T> {
    private val areacode = SaveUserInfo.instance.areaCode
    private val unitcode = SaveUserInfo.instance.unitCode
    private val homeModle: BaseModle

    init {
        homeModle = HomeModleImpl(this)
    }

    override fun getBannerUrls(which: Int) {
        homeView!!.showProgress()
        addSubscription(homeModle.getObservable<Banner>(ApiServiceManager.bannerUrl, which))
    }

    override fun getWorkInfo(which: Int) {
        addSubscription(homeModle.getObservable<Work>(ApiServiceManager.getWorkInfo(areacode), which))
    }

    override fun getNoticeInfo(which: Int) {
        //homeView.showProgress();
        addSubscription(homeModle.getObservable<Notice>(ApiServiceManager.getNoticeInfo("1", "1", "10", areacode), which))
    }

    override fun getQiuniuToken(which: Int) {
        addSubscription(homeModle.getObservable<BaseBean>(ApiServiceManager.qiuniuToken, which))
    }

    override fun onDestory() {
        onUnsubscribe()
        if (homeView != null) {
            homeView = null
        }
    }

    override fun onRequestSuccess(data: T, which: Int) {
        if (homeView != null) {
            homeView!!.hideProgress()
            homeView!!.onSuccess(data, which)
        }
    }

    override fun onRequestFail(throwable: Throwable) {
        if (homeView != null) {
            homeView!!.hideProgress()
            homeView!!.onFail(throwable)
        }
    }
}
